package cn.yunyichina.provider.pay.wechat.service;

import cn.yunyichina.provider.pay.wechat.entity.WechatAppletPayResponse;
import cn.yunyichina.utils.commons.RandomUtil;
import cn.yunyichina.utils.security.SignUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Lullaby on 2016/12/13.
 */
public class BaseAppletService extends BaseService {

    /**
     * 构建小程序支付出参
     *
     * @param response
     * @param apiSecret
     * @return
     */
    protected Map<String, String> buildAppletPayResponse(WechatAppletPayResponse response, String apiSecret) {
        final HashMap<String, String> result = new HashMap<>();

        result.put("appId", response.getAppid());
        result.put("timeStamp", String.valueOf(System.currentTimeMillis() / 1000));
        result.put("nonceStr", RandomUtil.getRandomALLChar(32));
        result.put("package", "prepay_id=" + response.getPrepayId());
        result.put("signType", "MD5");
        result.put("paySign", SignUtil.wechatPaySign(result, apiSecret));
        result.remove("appId");

        return result;
    }

}
